<?php

namespace App\Http\Controllers;

use App\Test1116;
use App\Test1116role;
use App\Test1116s;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Test1116Controller extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 登录界面
     * 2020年11月16日09点30分
     */
    public function login(){
        return view('test1116login');
    }

    /**
     * @param Request $request
     * 登录方法
     * 2020年11月16日09点31分
     */
    public function logindo(Request $request){
        $data['username'] = $request['username'];
        $data['password'] = $request['password'];
        $res = Test1116s::GetOne($data);
        if($res){
            echo "<script>alert('登录成功')</script>";
            $request->session()->put('username', $data['username']);
            header("refresh:0;url=show");
        }else{
            echo "<script>alert('登录失败')</script>";
            header("refresh:0;url=login");
        }
    }

    /**
     * @param Request $request
     * 退出登录
     * 删除session值
     * 2020年11月16日09点30分
     */
    public function delSess(Request $request){
        $request->session()->forget('username');
        echo "<script>alert('退出成功！')</script>";
        header("refresh:0;url=login");
    }

    /**
     * @param Request $request
     * 展示页面
     * 2020年11月16日09点31分
     */
    public function show(Request $request){

        if ($request->session()->has('username')) {
            $where['username'] = $request->session()->get('username');
            $data1 = Test1116s::GetOne1($where)->toArray();
            $user_id = $data1[0]['user_id'];
            $user = $data1[0]['username'];
            $data = Test1116::GetAll($user_id)->toArray();
            $newarr = [];
            foreach($data as $k=>$v){
                if($v['pid'] == 0){
                    $v['level'] = 1;
                    $newarr[] = $v;
                }else{
                    $v['level'] = 2;
                    $newarr[] = $v;
                }
            }
//            foreach($data as $v){
//                if($v['pid'] == 0){
//                    $v['level'] = 1;
//                    $newarr[] = $v;
//                    foreach($data as $value){
//                        if($value['pid'] = $v['access_id']){
//                            $v['level'] = 2;
//                            $newarr[] = $value;
//                        }
//                    }
//                }
//            }
            return view('test1116show',['data'=>$newarr,'user'=>$user,'id'=>$user_id]);
        }else{
            echo "<script>alert('请先登录！')</script>";
            header("refresh:0;url=login");
        }
    }
    public function roleadd(){
        return view('test1116add');
    }
    public function roleadddo(Request $request){
        $data['rolename'] = $request['rolename'];
        $res = Test1116role::add($data);
        if($res){
            echo "<script>alert('添加成功!')</script>";
            header("refresh:0;url=show");
        }else{
            echo "<script>alert('添加失败!')</script>";
            header("refresh:0;url=add");
        }
    }
    public function roleshow(){
        $user_id = 1;
        $data = Test1116::GetAll($user_id)->toArray();
        $newarr = [];
        foreach($data as $k=>$v){
            if($v['pid'] == 0){
                $v['level'] = 1;
                $newarr[] = $v;
            }else{
                $v['level'] = 2;
                $newarr[] = $v;
            }
        }
    }
}
